package com.shsoft.task;

import com.shsoft.MonitorApplication;
import com.shsoft.config.PCStatusConstant;
import com.shsoft.entity.HardWareInfo;
import com.shsoft.serial.SerialPortManager;
import javafx.application.Platform;


public class SendDataTask implements Runnable{

    private int backIndex;

    private int totalPart;

    private boolean synchronous = false;

    private byte[] poll;

    @Override
    public void run() {
        while (true){
            try{
                if(MonitorApplication.priorityDataArray.size() > 0){
                    poll = MonitorApplication.priorityDataArray.poll();
                    waitingForAnswer();
                    SerialPortManager.sendToPort(MonitorApplication.serialport, poll);
                    long startTime = System.currentTimeMillis();
                    while (true){
                        if(System.currentTimeMillis() > startTime + PCStatusConstant.RESPONSE_TIMEOUT){
                            break;
                        }
                        if(MonitorApplication.msgOK.get()) {
                            if(MonitorApplication.msgAnswer.equals(PCStatusConstant.RESPONSE_WRONG)){
                                waitingForAnswer();
                                SerialPortManager.sendToPort(MonitorApplication.serialport, poll);
                            }else if(MonitorApplication.msgAnswer.equals(PCStatusConstant.RESPONSE_OK)){
                                break;
                            }
                        }
                        Thread.sleep(1);
                    }
                    if(poll.length == 18){
                        backIndex = 0;
                        synchronous = true;
                        int animFrame = poll[4];
                        int animWidth = poll[7] * 256 + poll[8];
                        int animHeight = poll[9] * 256 + poll[10];
                        if(animWidth * animHeight == 480 * 40){
                            totalPart = 8 + animFrame;
                        }else {
                            totalPart = 8;
                        }
                    }else if(poll.length == PCStatusConstant.BACK_IMAGE_PART_SIZE){
                        backIndex++;
                    }
                    if(synchronous && backIndex == totalPart){
                        Thread.sleep(1000);
                        HardWareInfo.refreshAll = true;
                        MonitorApplication.passNormalData.set(true);
                        Platform.runLater(() -> {
                            MonitorApplication.mainController.loading.close();
                        });
                        synchronous = false;
                    }
                }else if(MonitorApplication.normalDataArray.size() > 0){
                    poll = MonitorApplication.normalDataArray.poll();
                    SerialPortManager.sendToPort(MonitorApplication.serialport, poll);
                }else {
                    Thread.sleep(10);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void waitingForAnswer(){
        MonitorApplication.msgOK.set(false);
        MonitorApplication.msgAnswer = "";
    }
}
